<?php
/**
 * Created by PhpStorm.
 * User: Apache
 *  1 展会活动 2 环节
 *
 */

namespace app\api\controller\activemanage;
use app\api\controller\common\ApiBase;
use think\Db;
use think\Model;
use app\api\model\Admin as AdminModel;
use app\api\controller\common\CommonPlus;
use think\Controller; // dengh quxiao
// Controller  ApiBase （dengl）
class Zhactive extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->admin = session('admin');
        //$this->model = model('CompanyPublic');
    }


    /**
     * 根据公海id获得获取
     * @return \think\response\Json|void
     *  http://dxshowxya.for.pub/index.php?s=/api/basemanage/Yuyuemanage/getList
     */

    public function getTabName($exhibition_id){
        $result=db('exhibition')->where(['id'=>$exhibition_id])->find();
        if($result){
            $tabName=$result['create_tablename'];
        }else{
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        return $tabName;
    }

    /**
     *
     * @return \think\response\Json|void
     *  comppub
     *  不知道跟展会环节有啥关联  ？
     */

    public function getList(){
        $page = $this->request->param('page');
        $page = max(1,intval($page));
        $pagesize =$this->request->param('pageSize')==""?20:$this->request->param('pageSize');
        $exhibition_id =$this->request->param('exhibition_id');
        $status =$this->request->param('status');
        $status_show =$this->request->param('status_show');
        $signup_type =$this->request->param('signup_type');
        $type_hdlx =$this->request->param('type_hdlx');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        $param=" and m.is_del=0 and m.exhibition_id=$exhibition_id ";
        if($status>0){
            $param.=" and m.status=$status ";
        }
        if($status_show>=0){
            $param.=" and m.status_show=$status_show ";
        }
        if($signup_type>=0){
            $param.=" and m.signup_type=$signup_type ";
        }

        if($type_hdlx>0){
            $param.=" and m.type_hdlx=$type_hdlx ";
        }

        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and (m.title like '%$keyword%') ";
        }
        $param.=" order by m.id desc";

        $sql2="select m.*,e.title as exhibition_name";
        $sql2.=" from crm_zh_active m ";
        $sql2.=" left join crm_exhibition as e on e.id=m.exhibition_id";
        //$sql2.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql2.=" where 1=1 $param";
        $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
        $list=Db::query($sql2);
        $sql21=" select count(id) as count1 ";
        $sql21.=" from crm_zh_active as m where 1=1 $param ";
        $total1=Db::query($sql21);
        $total=$total1[0]['count1']; //st
        return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total]]);
    }


    /**
     *  添加
     */
    public function add(){
        $com =new CommonPlus();
        $exhibition_id = $this->request->param('exhibition_id');
        $id_zs = $this->request->param('id_zs');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        $data['create_time']=time();
        // $data['id_zs']=$id_zs;
        $data['exhibition_id']=$exhibition_id;
        $data['title']=$this->request->param('title');   // 新品名称中文
        $data['type_hdlb']=$this->request->param('type_hdlb');// 活动类别
        $data['type_hdlx']=$this->request->param('type_hdlx'); // 类型
        $data['file']=$this->request->param('file'); // 类型
        $time_st=$this->request->param('time_st');
        $time_ed=$this->request->param('time_ed');
        if(!empty($time_ed) && !empty($time_ed)){
            $data['time_st']=strtotime($time_st);// 开始时间
            $data['time_ed']=strtotime($time_ed); // 结束时间
            if($data['time_st']>$data['time_ed']){
                return json(['code' => 0, 'msg' => '时间选择有误']);
            }
        }
        $data['addr']=$this->request->param('addr');  // 地址
        $data['org_zuzhi']=$this->request->param('org_zuzhi');  // 组织
        $data['intro']=$this->request->param('intro');  //  介绍
        $data['content']=$this->request->param('content');  // 内容
        $data['file']=$this->request->param('file');  // 文件
        $data['status']=$this->request->param('status');  // 文件
        $data['status_show']=$this->request->param('status_show');  // 文件
        $data['status_show']=$data['status_show']?$data['status_show']:0;
        $data['active_log']=$this->request->param('active_log');  // 文件
        $data['active_design_phone']=$this->request->param('active_design_phone');  // 文件
        $data['active_design_web']=$this->request->param('active_design_web');  // 文件
        $data['addr']=$this->request->param('addr');  // 文件
        $data['signup_type']=$this->request->param('signup_type');  // 文件

        // ---
        $res=$com->insert_com_dan($data, '', 'zh_active');
        if(!$res){
            return json(['code'=>0,'msg'=>'操作失败','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }


    /**
     * 删除
     */
    public function del(){
        $com =new CommonPlus();
        $id = $this->request->param('id');
        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $r = db('zh_active')->where(['id'=>$id])->find();
        if(!$r){
            return json(['code'=>0,'msg'=>'记录不存在','data'=>'']);
        }
        $where['id'] = $id;
        $data['is_del']=1;
        $r=db('zh_active')->where(['id'=>$id])->update($data);
        if(!$r){
            return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }
        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
    }
    /**
     * 批量删除展品管理
     */
    public function delall(){
        $com =new CommonPlus();
        $tabName='zh_active';
        $id = $this->request->param('id');

        if(empty($id)){
            return json(['code'=>0,'msg'=>'参数有误','data'=>'']);
        }
        $hjWhere['id'] = array('in', $id);
        $hjWhere['is_del'] = 0;
        $hjres=db('zh_active_huanjie')->where($hjWhere)->select();
        if(!empty($hjres)){
            return json(['code'=>0,'msg'=>'已关联活动环节','data'=>'']);
        }
        $ids = explode(',',$id);
        foreach ($ids as $id){
            $where['id'] = $id;
            $r=$com->sel_com_one('222', $where, $tabName);

            if(!$r){
                return json(['code'=>0,'msg'=>'id:'.$id.'记录不存在,批量删除失败','data'=>'']);
            }else{
                $where1['id'] = $id;
                $data['is_del']=1;
                $r=$com->edit_com_fxlist($data, $where1, $tabName);
                if(!$r){
                    return json(['code'=>0,'msg'=>'id:'.$id.'删除失败','data'=>'']);
                }
            }


        }

        return json(['code'=>1,'msg'=>'操作成功','data'=>'']);

        Db::startTrans();
        try {
            // $map['id'] = array('in',$ids);
            // $r = $this->model->where($map)->delete();
            // Db::commit();
            // return json(['code'=>1,'msg'=>'操作成功','data'=>'']);
        } catch (\Exception $e) {
            //如获取到异常信息，对所有表的删、改、写操作，都会回滚至操作前的状态：
            // Db::rollback();
            //  return json(['code'=>0,'msg'=>'操作失败，请重试','data'=>'']);
        }


    }


    /**
     * 基本信息修改
     * @return \think\response\Json|void
     */
    public function edit()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        //$create_uid  = $this->admin['id'];
        $data['update_time']=time();

        // $data['id_zs']=$id_zs;
        $data['exhibition_id']=$this->request->param('exhibition_id');
        $data['title']=$this->request->param('title');   // 新品名称中文
        $data['type_hdlb']=$this->request->param('type_hdlb');// 活动类别
        $data['type_hdlx']=$this->request->param('type_hdlx'); // 类型
        $data['file']=$this->request->param('file'); // 类型
        $time_st=$this->request->param('time_st');
        $time_ed=$this->request->param('time_ed');
        if(!empty($time_ed) && !empty($time_ed)){
            $data['time_st']=strtotime($time_st);// 开始时间
            $data['time_ed']=strtotime($time_ed); // 结束时间
            if($data['time_st']>$data['time_ed']){
                return json(['code' => 0, 'msg' => '时间选择有误']);
            }
        }

        $data['addr']=$this->request->param('addr');  // 地址
        $data['org_zuzhi']=$this->request->param('org_zuzhi');  // 组织
        $data['intro']=$this->request->param('intro');  //  介绍
        $data['content']=$this->request->param('content');  // 内容
        $data['file']=$this->request->param('file');  // 文件
        $data['status']=$this->request->param('status');  // 文件
        $data['status_show']=$this->request->param('status_show');  // 文件
        $data['status_show']=$data['status_show']?$data['status_show']:0;
        $data['active_log']=$this->request->param('active_log');  // 文件
        $data['active_design_phone']=$this->request->param('active_design_phone');  // 文件
        $data['active_design_web']=$this->request->param('active_design_web');  // 文件
        $data['addr']=$this->request->param('addr');  // 文件
        $data['signup_type']=$this->request->param('signup_type');  // 文件
        $is_group=$this->request->param('is_group');
        $data['is_group']=$is_group?$is_group:1;
        $where['id'] = $id;
        $tbname="zh_active";
        //$data2=array('pwd'=>md5($pwd),'account'=>'cccccccccc');
        $where2=array('id'=>$id,'company_name'=>'test');
        $r=$com->edit_com_fxlist($data, $where, $tbname);

        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功']);
    }

    // 明细
    public function getDetail()
    {
        $com =new CommonPlus();
        // com
        $id = $this->request->param('id');
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '参数有误，请核查']);
        }
        $where['id'] = $id;
        $tbname="zh_active";
        $r=$com->sel_com_one('222', $where, $tbname);
        if (!$r) {
            return json(['code' => 0, 'msg' => '操作失败，请重试']);
        }

        return json(['code' => 1, 'msg' => '操作成功','data'=>$r]);
    }




    // the end
    public function export()
    {
        $exhibition_id =$this->request->param('exhibition_id');
        $ids =$this->request->param('ids');
        if(empty($exhibition_id)){
            return json(['code'=>0,'msg'=>'缺少关键字段','data'=>'']);
        }
        $param=" and m.is_del=0";
        if(empty($ids)){
            if(!empty($exhibition_id)){
                $param.=" and m.exhibition_id=$exhibition_id ";
            }
            $keyword =input('keyword');
            if (!empty($keyword)) {
                $param.=" and (c.company_name like '%$keyword%') ";
            }
        }else{
            $param.=" and m.id in ($ids)";
        }
        $tabName =$this->getTabName($exhibition_id);
        $sql2="select m.*,e.title as exhibition_name,c.company_name as company_name_zs,c.id as company_id";
        $sql2.=" from crm_zsgoodszp m ";
        $sql2.=" left join crm_exhibition as e on e.id=m.exhibition_id";
        $sql2.=" left join crm_".$tabName." as c on c.id=m.id_zs";
        $sql2.=" where 1=1 $param";
        $list=Db::query($sql2);
        $columns = array(
            array('title' => '新品中文名称', 'field' => 'title', 'width' => 12),
            array('title' => '新品英文名称', 'field' => 'title_en', 'width' => 12),
            array('title' => '新品报价', 'field' => 'price', 'width' => 12),
            array('title' => '中文介绍', 'field' => 'positions_en', 'width' => 12),
            array('title' => '中文公司', 'field' => 'intro', 'width' => 12),
            array('title' => '英文介绍', 'field' => 'intro_en', 'width' => 12),
            array('title' => '新品照片', 'field' => 'pic', 'width' => 12),
            array('title' => '所属展会', 'field' => 'exhibition_name', 'width' => 12),
            array('title' => '所属展商', 'field' => 'company_name_zs', 'width' => 12),
        );
        $title = "展品明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        return json(['code'=>1,'msg'=>'操作成功','data'=>['columns'=>$columns,'filename'=>$filename,'list'=>$list]]);
    }


    //报名数据
    //报名数据
    public function signupList(){
        if (request()->isPost()) {
            $post = input('post.');
            $active_id = isset($post['active_id']) && $post['active_id'] != '' ? $post['active_id'] : 0;
            $page = isset($post['page']) && $post['page'] != '' ? $post['page'] : 1;
            $pageSize = isset($post['pageSize']) && $post['pageSize'] != '' ? $post['pageSize'] : 10;
            $keyWord = isset($post['keyWord']) && $post['keyWord'] != '' ? $post['keyWord'] : null;
            $company_name = isset($post['company_name']) && $post['company_name'] != '' ? $post['company_name'] : null;
            $play_status = isset($post['play_status']) && $post['play_status'] != '' ? $post['play_status'] : 0;
            $billing_status = isset($post['billing_status']) && $post['billing_status'] != '' ? $post['billing_status'] : 0;
            $signup_type = isset($post['signup_type']) && $post['signup_type'] != '' ? $post['signup_type'] : 0;
            $active=db('zh_active')->where(['id'=>$active_id,'is_del'=>0])->find();
            if(!$active){
                return json(array('code' => '0', 'msg' => '活动数据错误'));
            }
            $param=" and a.is_delete=0 and a.active_id=".$active_id;
            if($keyWord){
                $param.=" and a.name_cn LIKE '%$keyWord%' or a.mobile_person LIKE '%$keyWord%' ";
            }
            if($company_name){
                $param.=" and a.company_name LIKE '%$company_name%' ";
            }

            if($play_status == 1){
                $param.=" and a.play_status=0";
            }else if($play_status == 2){
                $param.=" and a.play_status=1";
            }

            if($billing_status == 1){
                $param.=" and a.billing_status=0";
            }else if($billing_status == 2){
                $param.=" and a.billing_status=1";
            }else if($billing_status == 3){
                $param.=" and a.billing_status=2";
            }

            if($signup_type>0){
                $param.=" and a.signup_type=".$signup_type;
            }

            $sql="SELECT a.*,c.title as active_titles,q.title as channel_name FROM crm_zh_active_signup as a ";
            $sql.=" LEFT JOIN crm_zh_active as c ON c.id=a.active_id";
            $sql.=" LEFT JOIN crm_active_zsqudao as q ON q.id=a.channel_id";
            $sql.=" where 1=1 $param";
            $sql.=" order by a.id desc";
//            var_dump($sql);die();
            $total1=Db::query($sql);
            $sql.=" limit ".($page-1)*$pageSize.",$pageSize";
            $list=Db::query($sql);
            $total=count($total1); //st
            return json(['code'=>1,'msg'=>'success','data'=>['list'=>$list,'count'=>$total,'active'=>$active]]);
        }else{
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 添加开票记录
     */
    public function addOrEdit(){
        if (request()->isPost()) {
            $admin = session('admin');
            $admin = AdminModel::get(['id' => $admin['id']]);
            $post = input('post.');
            if (empty($admin)) {
                return json(array('code' => '0', 'msg' => '请先登录或联系管理员注册账号'));
            }
            $signup_id = isset($post['signup_id']) && $post['signup_id'] != '' ? $post['signup_id'] : 0;
            $financeRes=db('zh_active_signup')->where(['id'=>$signup_id])->find();
            if(!$financeRes){
                return json(array('code' => '0', 'msg' => '报名记录错误'));
            }
            $exhibition_id=isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : '';
            if(!$exhibition_id){
                return json(array('code' => '0', 'msg' => '展会记录错误'));
            }
            $active_id=isset($post['active_id']) && $post['active_id'] != '' ? $post['active_id'] : '';
            if(!$active_id){
                return json(array('code' => '0', 'msg' => '活动记录错误'));
            }
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : null;
            $billing_type = isset($post['billing_type']) && $post['billing_type'] != '' ? $post['billing_type'] : 1;
            $tax_number = isset($post['tax_number']) && $post['tax_number'] != '' ? $post['tax_number'] : '';
            $bank_title = isset($post['bank_title']) && $post['bank_title'] != '' ? $post['bank_title'] : '';
            $bank_number = isset($post['bank_number']) && $post['bank_number'] != '' ? $post['bank_number'] : '';
            $company_phone = isset($post['company_phone']) && $post['company_phone'] != '' ? $post['company_phone'] : '';
            $company_address = isset($post['company_address']) && $post['company_address'] != '' ? $post['company_address'] : '';
            $billing_mode = isset($post['billing_mode']) && $post['billing_mode'] != '' ? $post['billing_mode'] : 1;
            $billing_money = isset($post['billing_money']) && $post['billing_money'] != '' ? $post['billing_money'] : '';
            $receiving_name = isset($post['receiving_name']) && $post['receiving_name'] != '' ? $post['receiving_name'] : '';
            $receiving_phone = isset($post['receiving_phone']) && $post['receiving_phone'] != '' ? $post['receiving_phone'] : '';
            $receiving_address = isset($post['receiving_address']) && $post['receiving_address'] != '' ? $post['receiving_address'] : '';
            $company_name = isset($post['company_name']) && $post['company_name'] != '' ? $post['company_name'] : '';
            $data['billing_type']=$billing_type;
            $data['tax_number']=$tax_number;
            $data['bank_title']=$bank_title;
            $data['bank_number']=$bank_number;
            $data['company_phone']=$company_phone;
            $data['company_address']=$company_address;
            $data['billing_mode']=$billing_mode;
            $data['billing_money']=$billing_money;
            $data['receiving_name']=$receiving_name;
            $data['receiving_phone']=$receiving_phone;
            $data['receiving_address']=$receiving_address;
            $data['create_time']=time();
            $data['update_time']=time();
            $data['signup_id']=$signup_id;
            $data['active_id']=$active_id;
            $data['exhibition_id']=$exhibition_id;
            $data['member_id']=$admin['id'];
            $data['company_name']=$company_name;
            $data['billing_status']=1;
            $res='';
            if($id){
                $search=db('zh_active_billing')->where(['id'=>$id])->find();
                if(!$search){
                    return json(array('code' => '0', 'msg' => '数据错误'));
                }
                $res=db('zh_active_billing')->where(['id'=>$id])->update($data);
            }else{
                $res=db('zh_active_billing')->insert($data);
            }

            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            if($financeRes['billing_status'] != 1){
                db('zh_active_signup')->where(['id'=>$signup_id])->update(['billing_status'=>1]);
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 开票记录详情
     */
    public function billingDetail(){
        if (request()->isPost()) {
            $post = input('post.');
            $signup_id = isset($post['signup_id']) && $post['signup_id'] != '' ? $post['signup_id'] : 0;
            $financeRes=db('zh_active_billing')->where(['signup_id'=>$signup_id,'is_delete'=>0])->find();
            return json(array('code' => '1', 'msg' => '操作成功','data'=>$financeRes));
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //标记为已缴费
    public function setPlayStatus(){
        if (request()->isPost()) {
            $post = input('post.');
            $ids = isset($post['id']) && $post['id'] != '' ? $post['id'] : '';
            if(!$ids){
                return json(array('code' => '0', 'msg' => '数据有误'));
            }
            $res=db('zh_active_signup')->where(['id'=>array('in',$ids),'play_status'=>0])->update(['play_status'=>1]);
            if(!$res){
                return json(array('code' => '0', 'msg' => '操作失败'));
            }
            return json(array('code' => '1', 'msg' => '操作成功'));
        }else{
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    public function channelList(){
        $exhibition_id =$this->request->param('exhibition_id');
        $param=" and w.is_delete=0 ";
        if(!empty($exhibition_id)){
            $param.=" and w.exhibition_id=$exhibition_id ";
        }
        $keyword =input('keyword');
        if (!empty($keyword)) {
            $param.=" and w.title like '%$keyword%' ";
        }

        $sql2="select w.*,c.id as exhibition_id,c.title as exhibition_title";
        $sql2.=" from crm_active_zsqudao as w LEFT JOIN crm_exhibition as c ON w.exhibition_id=c.id where 1=1 $param";
        $sql2.=" order by w.id desc ";
        $list=Db::query($sql2);

        return json(['code'=>1,'msg'=>'success','data'=>$list]);
    }


    public function adminAddUser(){
        if (request()->isPost()) {
            Db::startTrans();
            try {
                $com =new Zhactiveweb();
                $active_id = $this->request->param('active_id');
                $res=db('zh_active')->where(['id'=>$active_id,'is_del'=>0,'status_show'=>0])->find();
                if(!$res){
                    return json(['code'=>0,'msg'=>'活动数据错误']);
                }
                $mobile_person = $this->request->param('mobile_person');
                if(empty($mobile_person)){
                    return json(['code'=>0,'msg'=>'请填写电话']);
                }
                $exhibition_id = $this->request->param('exhibition_id');
                $exhibition=db('exhibition')->where(['is_delete'=>0,'id'=>$exhibition_id])->find();
                if(!$exhibition){
                    return json(['code'=>0,'msg'=>'展会信息错误']);
                }

                $resMobile=db('zh_active_signup')->where(['mobile_person'=>$mobile_person,'is_delete'=>0,'active_id'=>$active_id])->find();
                if($resMobile){
                    return json(['code'=>0,'msg'=>'此电话已报名']);
                }
                $name_cn = $this->request->param('name_cn');
                if(empty($name_cn)){
                    return json(['code'=>0,'msg'=>'请填写姓名']);
                }
                $id_side = $this->request->param('id_side');
                if(empty($id_side)){
                    return json(['code'=>0,'msg'=>'请填写证件号码']);
                }
                $id_positive=$this->request->param('id_positive');
                if(empty($id_positive)){
                    return json(['code'=>0,'msg'=>'请填写证件类型']);
                }
                if($id_positive == '中国居民身份证'){
                    $nameOrId=$com->getUserId($name_cn,$id_side);
                    if($nameOrId['code']==0){
                        return json(['code'=>0,'msg'=>'身份证：'.$nameOrId['msg']]);
                    }
                }
                $company_name = $this->request->param('company_name');
                if(empty($company_name)){
                    return json(['code'=>0,'msg'=>'请填写公司名称']);
                }
                $department_cn = $this->request->param('department_cn');
                if(empty($department_cn)){
                    return json(['code'=>0,'msg'=>'请填写部门信息']);
                }
                $positions_cn = $this->request->param('positions_cn');
                if(empty($positions_cn)){
                    return json(['code'=>0,'msg'=>'请填写职位信息']);
                }
                $email = $this->request->param('email');
                if(empty($email)){
                    return json(['code'=>0,'msg'=>'请填写电子邮件']);
                }
                $is_pc=$this->request->param('is_pc');
                $channel_id=$this->request->param('channel_id');
                $group_invitation_type=$res['is_group'];
                $group_invitation_type=$group_invitation_type?$group_invitation_type:1;
                //开始事务
                //基础表填入
                $data['name_cn']=$name_cn;
                $data['company_name']=$company_name;
                $data['department_cn']=$department_cn;
                $data['email']=$email;
                $data['update_time']=time();
                $data['create_time']=time();
                $data['positions_cn']=$positions_cn;
                $data['mobile_person']=$mobile_person;
                $baseInfo=db('company_user')->where(['mobile_person'=>$mobile_person])->find();
                $baseId=false;
                if(!$baseInfo){
                    $baseId = db('company_user')->lock(true)->insertGetId($data);
                }else{
                    $baseId=$baseInfo['id'];
                }
                if(false === $baseId){
                    Db::rollback();
                    return json(array('code' => '0', 'msg' => '操作失败'));
                }

                //加入到观众列表
                $tabName=$com->getTabName($exhibition_id);
                $personInfo=db($tabName)->where(['person_id'=>$baseId])->find();
                $personRes=true;
                if(!$personInfo){
                    $max_id = db($tabName)->max('id');
                    $is_cart= $is_pc == 1?415000000:311000000;
                    $data['person_id']=$baseId;
                    $data['id_side']=$id_side;
                    $data['id_positive']=$id_positive;
                    $data['is_beforehand']=2;
                    $data['is_cart']=$is_cart+$max_id+1;
                    $data['channel_id']=$channel_id;
                    $data['status']=4;
                    $data['group_invitation_type']=$group_invitation_type;
                    if($group_invitation_type == 2){
                        $data['examine_type']=1;
                    }
                    $personRes = db($tabName)->lock(true)->insert($data);
                }else{
                    $updates['name_cn']=$name_cn;
                    $updates['id_side']=$id_side;
                    $updates['id_positive']=$id_positive;
                    $data['id_side']=$id_side;
                    $data['id_positive']=$id_positive;
                    if(!$personInfo['id_side'] || !$personInfo['id_positive']){
                        $personRes = db($tabName)->where(['id'=>$personInfo['id']])->update($updates);
                    }
                }
                if(false === $personRes){
                    Db::rollback();
                    return json(array('code' => '0', 'msg' => '操作失败'));
                }
                if(array_key_exists("person_id",$data)){unset($data['person_id']);}
                if(array_key_exists("is_beforehand",$data)){unset($data['is_beforehand']);}
                if(array_key_exists("is_cart",$data)){unset($data['is_cart']);}
                if(array_key_exists("status",$data)){unset($data['status']);}
                if(array_key_exists("group_invitation_type",$data)){unset($data['group_invitation_type']);}
                if(array_key_exists("examine_type",$data)){unset($data['examine_type']);}
                $data['channel_id']=$channel_id;
                $data['signup_type']=$this->request->param('signup_type');
                $data['signup_type']=$data['signup_type']?$data['signup_type']:0;
                $data['product']=$this->request->param('product');
                $data['ong_advantage']=$this->request->param('ong_advantage');
                $data['sales_volume']=$this->request->param('sales_volume');
                $data['qualifications']=$this->request->param('qualifications');
                $data['major_exporting_countries']=$this->request->param('major_exporting_countries');
                $data['cooperative_enterprise']=$this->request->param('cooperative_enterprise');
                $data['active_type']=$this->request->param('active_type');
                $data['join_active_type']=$this->request->param('join_active_type');
                $data['zy_class']=$this->request->param('zy_class');
                $data['industry']=$this->request->param('industry');
                $data['stores_number']=$this->request->param('stores_number');
                $data['purchase']=$this->request->param('purchase');
                $data['zy_management']=$this->request->param('zy_management');
                $sex=$this->request->param('sex');
                $data['sex']=$sex?$sex:'男';
                $data['active_id']=$active_id;
                $data['email']=$email;
                $data['exhibition_id']=$exhibition_id;
                $result=db('zh_active_signup')->lock(true)->insert($data);
                if(false === $result){
                    return json(['code'=>0,'msg'=>'操作失败']);
                }
                Db::commit();
                return json(array('code' => '1', 'msg' => '成功'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(array('code' => '0', 'msg' => $e->getMessage()));
            }
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    //渠道复用
    public function getChannelList(){
        $exhibition_id =$this->request->param('exhibition_id');
        $param=" and w.is_delete=0 ";
        if(!empty($exhibition_id)){
            $param.=" and w.exhibition_id=$exhibition_id ";
        }
        $sql2="select w.*";
        $sql2.=" from crm_active_zsqudao as w";
        $sql2.=" where 1=1 $param order by w.id desc ";
        $list=Db::query($sql2);
        return json(['code'=>1,'msg'=>'success','data'=>$list]);
    }






    // 导出  stj ce
    public function signupExport()
    {
        $post = input('post.');
        $active_id = isset($post['active_id']) && $post['active_id'] != '' ? $post['active_id'] : 0;
        $signup_type = isset($post['signup_type']) && $post['signup_type'] != '' ? $post['signup_type'] : 0;
        $ids = isset($post['ids']) && $post['ids'] != '' ? $post['ids'] : '';
        $active=db('zh_active')->where(['id'=>$active_id,'is_del'=>0,'status_show'=>0])->find();
        if(!$active){
            return json(array('code' => '0', 'msg' => '活动数据错误'));
        }
        $param=" and a.is_delete=0 AND a.signup_type=".$signup_type." and a.active_id=".$active_id;
        if($ids){
            $param.=" AND a.id in (".$ids.")";
        }
        $sql="SELECT a.*,c.title as active_titles,q.title as channel_name FROM crm_zh_active_signup as a ";
        $sql.=" LEFT JOIN crm_zh_active as c ON c.id=a.active_id";
        $sql.=" LEFT JOIN crm_active_zsqudao as q ON q.id=a.channel_id";
        $sql.=" WHERE 1=1 ".$param;
        $list=Db::query($sql);
        if(count($list)==0){
            return json(array('code' => '0', 'msg' => '暂无数据'));
        }
        foreach ($list as &$vv){
            $vv['play_status_name']=($vv['play_status'] == 0?'未缴费':'已缴费');
            $vv['billing_status_name']=($vv['billing_status'] == 0?'未申请':($vv['billing_status'] == 1?'已申请':'已开票'));
            $vv['create_time']=$vv['create_time']?date('Y-m-d',$vv['create_time']):'';
        }
        $columns = array(
            array('title' => 'ID', 'field' => 'id', 'width' => 12,'code'=>'A'),
            array('title' => '姓名', 'field' => 'name_cn', 'width' => 12,'code'=>'B'),
            array('title' => '手机', 'field' => 'mobile_person', 'width' => 12,'code'=>'D'),
            array('title' => '证件类型', 'field' => 'id_positive', 'width' => 12,'code'=>'D'),
            array('title' => '身份证/护照号码', 'field' => 'id_side', 'width' => 12,'code'=>'E'),
            array('title' => '电子邮件', 'field' => 'email', 'width' => 12,'code'=>'F'),
            array('title' => '公司名称', 'field' => 'company_name', 'width' => 12,'code'=>'L'),
            array('title' => '部门', 'field' => 'department_cn', 'width' => 24,'code'=>'N'),
            array('title' => '职位', 'field' => 'positions_cn', 'width' => 24,'code'=>'O'),
            array('title' => '活动名称', 'field' => 'active_titles', 'width' => 24),
            array('title' => '报名时间', 'field' => 'create_time', 'width' => 24),
        );

        $columns0 = array(
            array('title' => '渠道来源', 'field' => 'channel_name', 'width' => 24),
        );

        $columns1 = array(
            array('title' => '产品', 'field' => 'product', 'width' => 12,'code'=>'A'),
            array('title' => '优势单品', 'field' => 'ong_advantage', 'width' => 12,'code'=>'B'),
            array('title' => '销售额', 'field' => 'sales_volume', 'width' => 12,'code'=>'D'),
            array('title' => '资质认证', 'field' => 'qualifications', 'width' => 12,'code'=>'D'),
            array('title' => '主要出口国家', 'field' => 'major_exporting_countries', 'width' => 12,'code'=>'E'),
            array('title' => '主要合作单位', 'field' => 'cooperative_enterprise', 'width' => 12,'code'=>'F'),
        );


        $columns2 = array(
            array('title' => '性别', 'field' => 'sex', 'width' => 12,'code'=>'A'),
            array('title' => '所属行业', 'field' => 'industry', 'width' => 12,'code'=>'B'),
            array('title' => '年营业额', 'field' => 'sales_volume', 'width' => 12,'code'=>'D'),
            array('title' => '自有品牌经营品类', 'field' => 'industry_type', 'width' => 12,'code'=>'D'),
            array('title' => '门店数量', 'field' => 'stores_number', 'width' => 12,'code'=>'E'),
            array('title' => '意向采购品类', 'field' => 'purchase', 'width' => 12,'code'=>'F'),
        );

        $columns3 = array(
            array('title' => '缴费状态', 'field' => 'play_status_name', 'width' => 12,'code'=>'A'),
            array('title' => '开票状态', 'field' => 'billing_status_name', 'width' => 12,'code'=>'B'),
        );

        if($signup_type == 0){
            $columns=array_merge($columns,$columns0);
        }else if($signup_type==1){
            $columns=array_merge($columns,$columns1);
        }else if($signup_type == 2){
            $columns=array_merge($columns,$columns2);
        }

        if($active['type_hdlx'] == 2){
            $columns=array_merge($columns,$columns3);
        }

        $title = "观众明细";
        $filename = $title . date('Y-m-d-H-i', time()) . ".xls";
        return json(['code'=>1,'msg'=>'操作成功','data'=>['columns'=>$columns,'filename'=>$filename,'list'=>$list]]);
    }


}
